<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">系统管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="$router.back(-1)"
            >鱼池管理</span
          >
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">鱼池详情</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content">
      <div class="flex-bet">
        <el-form
          ref="searchForm"
          :inline="true"
          :model="searchForm"
          class="demo-form-inline"
        >
          <el-form-item label="">
            <el-select
              v-model="searchForm.sex"
              placeholder="请选择性别"
              @change="onSubmit"
              clearable
            >
              <el-option label="未鉴定" :value="0"></el-option>
              <el-option label="雄" :value="1"></el-option>
              <el-option label="雌" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="" prop="">
            <el-input v-model="searchForm.fishTypeYear" placeholder="请输入鱼类年份" clearable maxlength="50"
              show-word-limit></el-input>
          </el-form-item> -->
          <el-form-item>
            <div class="search-btn" @click="onSubmit"></div>
          </el-form-item>
          <el-form-item>
            <div class="reset-btn" @click="resetForm"></div>
          </el-form-item>
        </el-form>
        <div class="left-title flex">
          <!-- <div class="back-btn" @click="back()"></div> -->
          <div class="add-btn" @click="handleAdd()">添加鱼池详情</div>
        </div>
      </div>
      <NxTable
        :table-cols="tableCols"
        :table-data="tableData"
        :pag-ination="pagination"
        class="table-wrap"
        @refresh="refresh"
      >
        <template slot="sex" slot-scope="scope">
          <span v-if="scope.row.sex == 0">未鉴定</span>
          <span v-if="scope.row.sex == 1">雄</span>
          <span v-if="scope.row.sex == 2">雌</span>
        </template>
        <template slot="weightEvery" slot-scope="scope">
          <span>{{ (scope.row.weight / scope.row.num).toFixed(2) }}</span>
        </template>
        <template slot="operation" slot-scope="scope">
          <span @click="handleEdit(scope.row)" class="nx-btn nx-edit"
            >编辑</span
          >
          <span @click="handleDelete(scope.row)" class="nx-btn nx-del"
            >删除</span
          >
        </template>
      </NxTable>
    </div>
    <AddEditDialog
      :addEditId="addEditId"
      :fishpondId="searchForm.fishpondId"
      :addEditStatus="addEditStatus"
      :addEditIsDialog="addEditIsDialog"
    ></AddEditDialog>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import AddEditDialog from './components/addEditDialog.vue'
import { Page } from '@/utils/pagination'
import { getObjFishpondDetailList, delObjFishpondDetail } from '@/api/fishpond'
import { getSysFishTypes } from '@/api/system'

export default {
  components: {
    NxTable,
    AddEditDialog,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        fishpondId: '',
        fishTypeId: '',
        fishTypeYear: '',
      },
      // 表格字段
      tableCols: [
        {
          label: '鱼类名称',
          prop: 'fishName',
          align: 'center',
        },
        {
          label: '鱼类性别',
          slot: 'sex',
          align: 'center',
        },
        {
          label: '批次编码',
          prop: 'batchCode',
          align: 'center',
        },
        {
          label: '数量（尾）',
          prop: 'num',
          align: 'center',
        },
        {
          label: '卵径（mm）',
          prop: 'diameter',
          align: 'center',
        },
        {
          label: '初重（kg）',
          prop: 'weightPrime',
          align: 'center',
        },
        {
          label: '均重（kg）',
          slot: 'weightEvery',
          align: 'center',
        },
        {
          label: '总重（kg）',
          prop: 'weight',
          align: 'center',
        },

        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 160,
          fixed: 'right',
        },
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      fishTypeArray: [], // 鱼类集合
      userType: '', // 用户类型（0超级管理员，1公司管理员，2公司员工）
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.searchForm.userType = this.userType
    this.getList()
  },
  // 自定义方法
  methods: {
    // 返回
    back() {
      this.$router.push({
        path: `/fishpond/fishpondInfo`,
      })
    },
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        fishpondId: '',
        fishTypeId: '',
        fishTypeYear: '',
      }

      this.onSubmit()
    },
    // 查询列表
    getList() {
      this.searchForm.fishpondId = this.$route.query.id
      console.log(this.$route.query.id)
      getObjFishpondDetailList({ fishpondId: this.$route.query.id }).then(
        (res) => {
          if (res.code == 200) {
            this.tableData = res.data.data
            this.pagination.count = res.data.total
            this.getSysFishTypes()
          }
        }
      )
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delObjFishpondDetail({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg,
              })
              this.getList()
            }
          })
        })
        .catch((err) => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
    },
    getSysFishTypes() {
      getSysFishTypes().then((res) => {
        if (res.code == 200) {
          this.fishTypeArray = res.data
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;

  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;

    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }

    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
